// 日期时间格式化

Date.prototype.addDays = function(d){
    this.setDate(this.getDate() + d);
};

Date.prototype.addWeeks = function(w){
    this.addDays(w * 7);
};

Date.prototype.addMonths= function(m){
    var d = this.getDate();
    this.setMonth(this.getMonth() + m);
    if (this.getDate() < d){
    	this.setDate(0);
    }
};

Date.prototype.addYears = function(y){
    var m = this.getMonth();
    this.setFullYear(this.getFullYear() + y);
    if (m < this.getMonth()){
        this.setDate(0);
    }
};

Date.prototype.format = function(format){ 
	var o = { 
		"M+" : this.getMonth()+1, // month
		"d+" : this.getDate(), // day
		"h+" : this.getHours(), // hour
		"m+" : this.getMinutes(), // minute
		"s+" : this.getSeconds(), // second
		"q+" : Math.floor((this.getMonth()+3)/3), // quarter
		"S" : this.getMilliseconds() // millisecond
	}; 

	if(/(y+)/.test(format)) { 
		format = format.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length)); 
	} 

	for(var k in o) { 
		if(new RegExp("("+ k +")").test(format)) { 
			format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length)); 
		} 
	} 
	return format; 
};

/**
 * 简单时间比较函数，日期格式为yyyy-MM-dd HH:mm:ss
 * @param d1,d2
 */
var DateTimeCompare = function(d1, d2) {
	var result = false;
	
	var date1 = new Date(Date.parse(d1));  
	var date2 = new Date(Date.parse(d2)); 
	if (date1.getTime() >= date2.getTime()) {  
		result = true;  
    }  
	
	return result;
};  

/**
 * 数组删除元素,sku选项对象
 * @param s
 */
Array.prototype.remove = function(s) {   
    for (var i = 0; i < this.length; i++) {
        if (s && s.id == this[i].id){
            this.splice(i, 1);
            break;
        }
    }     
};

/**
 * Map删除功能
 * @param s
 */
Array.prototype.removekey = function(s) {   
  for (var i = 0; i < this.length; i++) {
    if (s == this[i]){
        this.splice(i, 1);
        break;
    }
  }     
};

/*
常量类
*/
var Constant = {
SEPARATOR : ",",
DEFAULTSKUOPTID : 0, //通用sku可选项id
};

/**
 * map对象
 * @returns
 */
var Map = function() {     
  /** 存放键的数组(遍历用到) */    
  this.keys = new Array();     
  /** 存放数据 */    
  this.data = new Object();     
  /**   
   * 放入一个键值对   
   * @param {String} key   
   * @param {Object} value   
   */
  this.put = function(key, value) {
      if(this.data[key] == null){ 
          this.keys.push(key);
      }  
      this.data[key] = value;     
  };
  /**   
   * 获取某键对应的值   
   * @param {String} key   
   * @return {Object} value   
   */    
  this.get = function(key) {     
      return this.data[key];     
  };  
  /**   
   * 删除一个键值对   
   * @param {String} key   
   */    
  this.remove = function(key) {     
    this.keys.removekey(key);
      this.data[key] = null;     
  };     
  /**   
   * 遍历Map,执行处理函数   
   *    
   * @param {Function} 回调函数 function(key,value,index){..}   
   */    
  this.each = function(fn){     
      if(typeof fn != 'function'){     
          return;     
      }     
      var len = this.keys.length;     
      for(var i=0;i<len;i++){     
          var k = this.keys[i];     
          fn(k,this.data[k],i);     
      }
  };
  /**   
   * 获取键值数组(类似Java的entrySet())   
   * @return 键值对象{key,value}的数组   
   */
  this.entrys = function() {     
      var len = this.keys.length;     
      var entrys = new Array(len);     
      for (var i = 0; i < len; i++) {     
          entrys[i] = {     
              key : this.keys[i],     
              value : this.data[this.keys[i]]     
          };     
      }     
      return entrys;     
  };     
  /**   
   * 判断Map是否为空   
   */    
  this.isEmpty = function() {     
      return this.keys.length == 0;     
  };     
  /**   
   * 获取键值对数量   
   */    
  this.size = function(){     
      return this.keys.length;     
  };     
  /**   
   * 重写toString    
   */    
  this.toString = function(){     
      var s = "{";     
      for(var i=0;i<this.keys.length;i++,s+=','){     
          var k = this.keys[i];     
          s += k+"="+this.data[k];     
      }     
      s+="}";     
      return s;     
  };     
};